package com.usian.utils.common;

import com.alibaba.fastjson.JSON;
import com.aliyun.imageaudit20191230.models.ScanImageResponse;
import com.aliyun.imageaudit20191230.models.ScanImageResponseBody;
import com.aliyun.tea.TeaException;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author baimugudu
 * @email 2415621370@qq.com
 * @date 2023/8/30 14:16
 */
@Slf4j
public class ScanImageUtil {

    public static final String accessKeyId = "LTAI5t9AAEksbQkcnzMQ3uQ5";
    public static final String accessKeySecret = "KoNtA0ld6DmlxLHfBD2fR3u8bCKyS2";


    /**
     * 使用AK&SK初始化账号Client
     * @param accessKeyId
     * @param accessKeySecret
     * @return Client
     * @throws Exception
     */
    public static com.aliyun.imageaudit20191230.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 必填，您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 必填，您的 AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // Endpoint 请参考 https://api.aliyun.com/product/imageaudit
        config.endpoint = "imageaudit.cn-shanghai.aliyuncs.com";
        return new com.aliyun.imageaudit20191230.Client(config);
    }


    public static boolean scanImage(String imageUrl) throws Exception {
        boolean flag = true;


        // 请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID 和 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
        // 工程代码泄露可能会导致 AccessKey 泄露，并威胁账号下所有资源的安全性。以下代码示例使用环境变量获取 AccessKey 的方式进行调用，仅供参考，建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/378657.html
        com.aliyun.imageaudit20191230.Client client = ScanImageUtil.createClient(accessKeyId,accessKeySecret);
        com.aliyun.imageaudit20191230.models.ScanImageRequest.ScanImageRequestTask task0 = new com.aliyun.imageaudit20191230.models.ScanImageRequest.ScanImageRequestTask()
                .setImageURL(imageUrl);
        com.aliyun.imageaudit20191230.models.ScanImageRequest scanImageRequest = new com.aliyun.imageaudit20191230.models.ScanImageRequest()
                .setTask(java.util.Arrays.asList(
                        task0
                ))
                .setScene(java.util.Arrays.asList(
                        "porn",
                        "terrorism"
                ));
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            // 复制代码运行请自行打印 API 的返回值
            ScanImageResponse scanImageResponse = client.scanImageWithOptions(scanImageRequest, runtime);
            log.info("scanImageResponse:{}",JSON.toJSONString(scanImageResponse));
            ScanImageResponseBody body = scanImageResponse.getBody();
            ScanImageResponseBody.ScanImageResponseBodyData data = body.getData();
            List<ScanImageResponseBody.ScanImageResponseBodyDataResults> results = data.getResults();
            List<ScanImageResponseBody.ScanImageResponseBodyDataResultsSubResults> scanImageResponseBodyDataResultsSubResults = null;
            for (int i = 0; i < results.size(); i++) {
                ScanImageResponseBody.ScanImageResponseBodyDataResults scanImageResponseBodyDataResults = results.get(i);
                 scanImageResponseBodyDataResultsSubResults =
                         scanImageResponseBodyDataResults.getSubResults().stream().filter(j -> !j.getSuggestion().equals("pass")).collect(Collectors.toList());
            }

            if(scanImageResponseBodyDataResultsSubResults==null||scanImageResponseBodyDataResultsSubResults.size()<=0){
                flag = false;
            }



        } catch (TeaException error) {
            // 如有需要，请打印 error
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 如有需要，请打印 error
            com.aliyun.teautil.Common.assertAsString(error.message);
        }

        return flag;

    }

    public static void main(String[] args_) throws Exception {
        List<String> args = java.util.Arrays.asList(args_);
        // 请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID 和 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
        // 工程代码泄露可能会导致 AccessKey 泄露，并威胁账号下所有资源的安全性。以下代码示例使用环境变量获取 AccessKey 的方式进行调用，仅供参考，建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/378657.html
        com.aliyun.imageaudit20191230.Client client = ScanImageUtil.createClient(accessKeyId,accessKeySecret);
        com.aliyun.imageaudit20191230.models.ScanImageRequest.ScanImageRequestTask task0 = new com.aliyun.imageaudit20191230.models.ScanImageRequest.ScanImageRequestTask()
                .setImageURL("https://m.360buyimg.com/babel/jfs/t1/178107/33/31045/29282/63969eecEb39fa1e5/0a30a2a378184c3b.jpg");
        com.aliyun.imageaudit20191230.models.ScanImageRequest scanImageRequest = new com.aliyun.imageaudit20191230.models.ScanImageRequest()
                .setTask(java.util.Arrays.asList(
                        task0
                ))
                .setScene(java.util.Arrays.asList(
                        "porn",
                        "terrorism"
                ));
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            // 复制代码运行请自行打印 API 的返回值
            ScanImageResponse scanImageResponse = client.scanImageWithOptions(scanImageRequest, runtime);
            log.info("scanImageResponse:{}",JSON.toJSONString(scanImageResponse));
            ScanImageResponseBody body = scanImageResponse.getBody();
            ScanImageResponseBody.ScanImageResponseBodyData data = body.getData();
            List<ScanImageResponseBody.ScanImageResponseBodyDataResults> results = data.getResults();
            results.forEach(i->{
                List<ScanImageResponseBody.ScanImageResponseBodyDataResultsSubResults> subResults = i.getSubResults();
                subResults.forEach(j->{
                    String suggestion = j.getSuggestion();
                    if(!suggestion.equals("pass")){

                    }

                });
            });
        } catch (TeaException error) {
            // 如有需要，请打印 error
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 如有需要，请打印 error
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
    }


}
